// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   wuxingchuang
 * @date     2025.06.26
 * @copyright Copyright (c) 2025
 */
#include "SUPORTParser.h"

#include <string>
#include <vector>

#include "boost/algorithm/string.hpp"

#include "DataStructure/Input/Constraint/SUPORTData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/ISUPORTService.h"

#include "Import/BDFImport/BDFParserBase.h"

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

BULKDATA_ENTRY_PARSER_REG (SUPORT);

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSUPORT (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    int curKey = 1;

    SG::DataStructure::FEM::SUPORTData tempData;

    bool hasTID = false;

    for (auto& curStrKey : dividedEntryInfo)
    {
        const bool isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curKey++;
            continue;
        }

        boost::trim (curStrKey);

        // TIDi DAMPi 成对解析
        if (curKey % 2 == 0)
        {
            tempData      = SG::DataStructure::FEM::SUPORTData ();
            tempData.m_id = convertToI64 (curStrKey);
            hasTID        = true;
        }
        else
        {
            if (hasTID)
            {
                tempData.m_C        = convertToI64 (curStrKey);
                tempData.m_domainId = 0;
                dbServiceFactory->get<ISUPORTService> ()->Add (tempData);
                hasTID = false;
            }
        }
        curKey++;
    }

    return SG::DataStructure::Common::Status::STATUS_Success;
}